(function($) {
  $.fn.modal = function(options) {
    options = $.extend({
      trigger: "modalTrigger",
      modals: "div.modal",
      olayBe: true,
      olay: {
        opacity: 0.7,
        background: "#000"
      },
      popPosition: {
        top: "Up",
        left: "Right"
      },
      smallfocuspadding: "7px",
      animationEffect: "fadeIn",
      animationSpeed: "1000",
      moveModalSpeed: "400",
      moveOnScroll: true,
      resizonwindow: true,
      docClose: true,
      closeByKeyboard: "27",
      close: ".closeBtn"
    }, options);
    var olay;
    var modals = $(options.modals);
    var currentModal;
    var isopen = false;
    var $this = this;
    var isOk = false;
    if (modals.css("zIndex") === "auto" || modals.css("zIndex") == 0) {
      modals.css("zIndex", 10000)
    }
    var olayBeindex = modals.css("zIndex") - 1;
    if (options.olayBe)
      olay = $("<div></div>").css({
        width: "100%",
        height: ($("body").height() > $(window).height()) ? $("body").height() : "100%",
        opacity: options.olay.opacity,
        background: options.olay.background,
        position: "fixed",
        top: 0,
        left: 0,
        zIndex: olayBeindex,
        display: "none"
      }).appendTo("body");
    $(options.trigger).bind("click", function(e) {
      e.preventDefault();
      if ($(options.trigger).length > 1) {
        // getmodal = $(this).attr("href");
        // currentModal = $(getmodal);
        currentModal = modals;
      } else {
        currentModal = modals;
      }
      openmodal()
    });
    $(options.close).bind("click", closemodal);
    if (options.docClose && options.olayBe)
      olay.bind("click", closemodal);
    if (options.moveOnScroll)
      $(window).bind("scroll", movemodal);
    if (options.resizonwindow)
      $(window).bind("resize", movemodal);
    if (options.closeByKeyboard > 0) {
      $("body").keyup(function(e) {
        if (e.which == options.closeByKeyboard) {
          closemodal();
        }
      });
    }

    function openmodal() {
      currentModal.css({
        top: popTop(options.popPosition.top, currentModal),
        left: popLeft(options.popPosition.left, currentModal)
      });
      if (isopen === false) {
        animationEffects[options.animationEffect]();
      } else {
        currentModal.show();
      }
      isopen = true;
    }

    function popTop(methodkey, methodpara) {
      var popTop_ = {
        Up: function(e) {
          return 0 + $(window).scrollTop();
        },
        Content: function(e) {
          return $(window).height() / 2 - e.outerHeight() / 2 + $(window).scrollTop();
        },
        windowDown: function(e) {
          return $(window).height() - e.outerHeight(true) + $(window).scrollTop();
        },
        bodyDown: function(e) {
          return $("body").height() + $(window).scrollTop();
        }
      }
      if (popTop_[methodkey]) {
        return popTop_[methodkey](methodpara);
      } else {
        return popTop_["Content"](methodpara);
      }
    }

    function popLeft(methodkey, methodpara) {
      var popLeft_ = {
        Left: function(e) {
          return 0 + $(window).scrollLeft();
        },
        Content: function(e) {
          return $(window).width() / 2 - e.outerWidth() / 2 + $(window).scrollLeft();
        },
        Right: function(e) {
          return $(window).width() - e.outerWidth(true) + $(window).scrollLeft();
        }
      }
      if (popLeft_[methodkey]) {
        return popLeft_[methodkey](methodpara);
      } else {
        return popLeft_["Content"](methodpara);
      }
    }
    var animationEffects = {
      fadeIn: function() {
        if (isOk) return false;
        if (options.olayBe)
          olay.fadeIn(options.animationSpeed);
        currentModal.delay(300).fadeIn(options.animationSpeed, function() {
          isOk = true;
        });
      },
    }
    var closeanimationEffects = {
      fadeIn: function() {
        if (!isOk) return false;
        isOk = false;
        modals.fadeOut(100, function() {
          if (options.olayBe)
            olay.fadeOut();
        });
      },
    }

    function movemodal() {
      setTimeout(function() {
        if (!isOk) return false;
        modals.stop(true).animate({
          top: popTop(options.popPosition.top, modals),
          left: popLeft(options.popPosition.left, modals)
        }, options.moveModalSpeed);
      }, 400);
    }

    function closemodal() {
      isopen = false;
      closeanimationEffects[options.animationEffect]();
    }
  }
})($)
